/**
 * @(#)CommonConvertor.java, 2018/2/3.
 * <p/>
 * Copyright 2018 Kerr Wang, Inc. All rights reserved.
 * KERRWANG PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package org.wangzx.exercise.meta.convertor;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author hzwangzuxiang(ahscwzx@163.com)
 */
public class CommonConvertor {

    private static final Logger logger = LoggerFactory.getLogger(CommonConvertor.class);

    /**
     * BO转PO
     *
     * @param bo
     * @param poClass
     * @return
     */
    public static <T> T boToPo(Object bo, Class<T> poClass) {
        if (bo == null) {
            return null;
        }
        T po = null;
        try {
            po = poClass.newInstance();
        } catch (Exception e) {
            logger.info("[boToPo error] poClass:{}", poClass.getName());
            return null;
        }
        BeanUtils.copyProperties(bo, po);
        return po;
    }

    /**
     * PO转BO
     *
     * @param po
     * @param boClass
     * @return
     */
    public static <T> T poToBo(Object po, Class<T> boClass) {
        if (po == null) {
            return null;
        }
        T bo = null;
        try {
            bo = boClass.newInstance();
        } catch (Exception e) {
            logger.info("[poToBo error] boClass:{}", boClass.getName());
            return null;
        }
        BeanUtils.copyProperties(po, bo);
        return bo;
    }

    /**
     * PO列表转BO列表
     *
     * @param poList
     * @param boClass
     * @return
     */
    public static <T> List<T> poToBo(List<?> poList, Class<T> boClass) {
        if (poList == null || poList.isEmpty()) {
            return new ArrayList<>();
        }

        List<T> boList = new ArrayList<T>(poList.size());
        for (Object po: poList) {
            T bo = poToBo(po, boClass);
            if (bo == null) {
                continue;
            }

            boList.add(bo);
        }
        return boList;
    }

    /**
     * BO列表转PO列表
     *
     * @param boList
     * @param poClass
     * @return
     */
    public static <T> List<T> boToPo(List<?> boList, Class<T> poClass) {
        if (boList == null || boList.isEmpty()) {
            return new ArrayList<>();
        }

        List<T> poList = new ArrayList<T>(boList.size());
        for (Object bo: boList) {
            T po = boToPo(bo, poClass);
            if (po == null) {
                continue;
            }

            poList.add(po);
        }
        return poList;
    }

    /**
     * VO转BO
     *
     * @param vo
     * @param boClass
     * @return
     */
    public static <T> T voToBo(Object vo, Class<T> boClass) {
        if (vo == null) {
            return null;
        }
        T bo = null;
        try {
            bo = boClass.newInstance();
        } catch (Exception e) {
            logger.info("[poToBo error] boClass:{}", boClass.getName());
            return null;
        }
        BeanUtils.copyProperties(vo, bo);
        return bo;
    }

    /**
     * VO转BO
     *
     * @param bo
     * @param voClass
     * @return
     */
    public static <T> T boToVo(Object bo, Class<T> voClass) {
        if (bo == null) {
            return null;
        }
        T vo = null;
        try {
            vo = voClass.newInstance();
        } catch (Exception e) {
            logger.info("[voClass error] voClass:{}", voClass.getName());
            return null;
        }
        BeanUtils.copyProperties(bo, vo);
        return vo;
    }

    /**
     * BO列表转PO列表
     *
     * @param boList
     * @param voClass
     * @return
     */
    public static <T> List<T> boToVo(List<?> boList, Class<T> voClass) {
        if (boList == null || boList.isEmpty()) {
            return new ArrayList<>();
        }

        List<T> voList = new ArrayList<T>(boList.size());
        for (Object bo: boList) {
            T vo = boToVo(bo, voClass);
            if (vo == null) {
                continue;
            }

            voList.add(vo);
        }
        return voList;
    }
}
